#include "mkmainwindow.h"
#include "mkprogramwidget.h"
#include "mkdeveloperwidget.h"
#include "mkparametersdialog.h"
#include "mkpiewidget.h"
#include "mktableview.h"

#include "mkcore/mkcore.h"

#include <QFile>
#include <QFileDialog>

#include <QMenu>
#include <QMenuBar>
#include <QVBoxLayout>
#include <QPushButton>
#include <QStatusBar>
#include <QLabel>

#include <QApplication>

#include <QDebug>

MKMainWindow::MKMainWindow(QWidget *parent)
	: QMainWindow(parent)
{
	QWidget *mainWidget = new QWidget;
	setCentralWidget(mainWidget);

	setMinimumWidth(440);
	init();
}

MKMainWindow::~MKMainWindow()
{

}

void MKMainWindow::init()
{
	createMenuBar();

	createTableView();

	createStatusBar();
}

void MKMainWindow::createMenuBar()
{
	QMenuBar *mMenu = menuBar();

	QMenu *fileMenu = mMenu->addMenu("Файл");

	QString openActName = "Открыть...";

	QAction *openFileAct = fileMenu->addAction(openActName);
	openFileAct->setShortcut(QKeySequence::Open);
	connect(openFileAct, SIGNAL(triggered()), this, SLOT(onOpenFile()));

	QAction *saveFileAct = fileMenu->addAction("Сохранить");
	saveFileAct->setShortcut(QKeySequence::Save);
	connect(saveFileAct, SIGNAL(triggered()), this, SLOT(onSaveFile()));

	QAction *saveFileAsAct = fileMenu->addAction("Сохранить как...");
	saveFileAsAct->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_S);
	connect(saveFileAsAct, SIGNAL(triggered()), this, SLOT(onSaveFileAs()));

	QMenu *settingsMenu = mMenu->addMenu("Настройки");

	QAction *addParametersAct = settingsMenu->addAction("Добавить параметр");
	connect(addParametersAct, SIGNAL(triggered()), this, SLOT(onAddParameters()));

	QMenu *visualMenu = mMenu->addMenu("Визуальное представление");

	QAction *visualTypeAct = visualMenu->addAction("По типам");
	connect(visualTypeAct, SIGNAL(triggered()), this, SLOT(onVisualType()));
	QAction *visualCategoryAct = visualMenu->addAction("По категориям");
	connect(visualCategoryAct, SIGNAL(triggered()), this, SLOT(onVisualCategory()));

	QMenu *helpMenu = mMenu->addMenu("Справка");

	QAction *aboutProgramAct = helpMenu->addAction("О программе...");
	connect(aboutProgramAct, SIGNAL(triggered()), this, SLOT(onAboutProgramm()));

	QAction *aboutDeveloperAct = helpMenu->addAction("О разработчиках...");
	connect(aboutDeveloperAct, SIGNAL(triggered()), this, SLOT(onAboutDeveloper()));
}

void MKMainWindow::createTableView()
{
	pMKTableView = new MKTableView;

	QVBoxLayout *centralLayout = new QVBoxLayout(centralWidget());

	centralLayout->addWidget(pMKTableView);

	QHBoxLayout *buttonLayout = new QHBoxLayout;
	centralLayout->addLayout(buttonLayout);
	QPushButton *addRowButton = new QPushButton("Добавить");
	QPushButton *removeRowButon = new QPushButton("Удалить");

	buttonLayout->addWidget(addRowButton);
	buttonLayout->addWidget(removeRowButon);
	buttonLayout->addStretch();

	QStatusBar *bar = new QStatusBar;
	buttonLayout->addWidget(bar);

	mBalanceText = QString("Текущий баланс: %1");
	pLblBalance = new QLabel(mBalanceText.arg(0));
	bar->addWidget(pLblBalance);

	connect(addRowButton, SIGNAL(clicked()), pMKTableView, SLOT(onAddRow()));
	connect(removeRowButon, SIGNAL(clicked()), pMKTableView, SLOT(onRemoveRow()));
	connect(pMKTableView, SIGNAL(balanceChanged(qint32)), this, SLOT(onBalanceChanged(qint32)));
}

void MKMainWindow::createStatusBar()
{
}

void MKMainWindow::onBalanceChanged(qint32 balance)
{
	pLblBalance->setText(mBalanceText.arg(QString::number(balance)));
}

void MKMainWindow::onOpenFile()
{
	QString fileName;
	fileName = QFileDialog::getOpenFileName(this, "Открыть...", "./", "MoneyKeeper Files (*.mkf)");
	pMKTableView->load(fileName);
}

void MKMainWindow::onSaveFile()
{
	if (!pMKTableView->save())
	{
		onSaveFileAs();
	}
}

void MKMainWindow::onSaveFileAs()
{
	QString fileName;
	fileName = QFileDialog::getSaveFileName(this, "Сохранить как...", "./", "MoneyKeeper Files (*.mkf)");
	if (!fileName.isEmpty())
	{
		pMKTableView->save(fileName);
	}
}

void MKMainWindow::onAddParameters()
{
	MKParametersDialog *parametersDialog = new MKParametersDialog();
	parametersDialog->exec();
}

void MKMainWindow::onVisualType()
{
	QMap<quint8, quint32> costForTypes = pMKTableView->costsForTypes();
	MKPieTypeWidget *pieWidget = new MKPieTypeWidget(costForTypes);
	pieWidget->show();
}

void MKMainWindow::onVisualCategory()
{
	QMap<quint8, QMap<quint8, quint32> > costForCategories = pMKTableView->costsForCategories();
	MKPieCategoryWidget *pieWidget = new MKPieCategoryWidget(costForCategories);
	pieWidget->show();
}

void MKMainWindow::onAboutProgramm()
{
	MKProgramWidget *about = new MKProgramWidget();
	about->show();
}

void MKMainWindow::onAboutDeveloper()
{
	MKDeveloperWidget *about = new MKDeveloperWidget();
	about->show();
}
